﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Model;
using Repository;
using UI.Areas.Admin.Models;
using UI.Helper;

namespace UI.Areas.Admin.Controllers
{
    public class FurnitureProductController : MyController
    {
        #region action

        public ActionResult Index()
        {
            var repository = RepositoryManager.CreateTKNTProductRepository();

            var lst = Paging(1, repository) ?? new List<tkntProduct>();

            return View(lst);
        }

        public ActionResult PagingData(int pageIndex)
        {
            var repository = RepositoryManager.CreateTKNTProductRepository();

            var lst = Paging(pageIndex, repository) ?? new List<tkntProduct>();

            return View("Index", lst);
        }

        public ActionResult Delete(int id)
        {
            var repository = RepositoryManager.CreateTKNTProductRepository();

            var objDelete = repository.HaveImage(id);

            if (objDelete == null)
            {
                TempData[Constants.MESSAGE] = "Không tìm thấy sản phẩm cần xóa.";
            }

            else
            {
                if (objDelete.Images.Count > 0)
                    TempData[Constants.MESSAGE] = "Đang có hình ảnh cho sản phẩm này nên không thể xóa.";

                else
                {
                    repository.Delete(objDelete);

                    TempData[Constants.MESSAGE] = "Đã xóa thành công";
                }
            }

            return RedirectToAction("Index");
        }

        public ActionResult DeleteAll(string id)
        {
            string[] arrId = id.Split(',');

            var i = 0;

            List<tkntProduct> lst = new List<tkntProduct>();

            var repository = RepositoryManager.CreateTKNTProductRepository();

            foreach (var s in arrId)
            {
                var objDelete = repository.HaveImage(Convert.ToInt32(s));

                if (objDelete == null)
                {
                    TempData[Constants.MESSAGE] = "Không tìm thấy sản phẩm cần xóa.";

                    break;
                }
                else
                {
                    if (objDelete.Images.Count > 0)
                    {
                        TempData[Constants.MESSAGE] = "Đang có hình ảnh cho sản phẩm này nên không thể xóa.";
                        break;
                    }
                    else
                        lst.Add(objDelete);
                }

                i++;
            }

            if (i == arrId.Length)
            {
                foreach (var category in lst)
                    repository.Delete(category);

                TempData[Constants.MESSAGE] = "Đã xóa thành công";
            }

            return RedirectToAction("Index");
        }

        public ActionResult Add()
        {
            var repository = RepositoryManager.CreateTKNTCategoryRepository();

            var model = new FurnitureProductModel { Categories = repository.GetAll() };

            return View(model);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Add(FurnitureProductModel productModel)
        {
            var catRepository = RepositoryManager.CreateTKNTCategoryRepository();

            var proRepository = RepositoryManager.CreateTKNTProductRepository();

            if (productModel != null)
            {
                var objCategory = catRepository.GetById(productModel.CategoryId);

                if (objCategory != null)
                {
                    var objProduct = new tkntProduct()
                    {
                        Category = objCategory,
                        Area = productModel.Area,
                        ViDescription = productModel.ViDescription,
                        EnDescription = productModel.EnDescription,
                        IsHot = productModel.IsHost,
                        ViInvestorName = productModel.ViInvestorName,
                        EnInvestorName = productModel.EnInvestorName,
                        ViInvestorDescription = productModel.ViInvestorDescription,
                        EnInvestorDescription = productModel.EnInvestorDescription,
                    };

                    proRepository.Insert(objProduct);

                    //productModel.ProductId = proRepository.SearchByDescription(productModel.ViDescription, Culture.Name)[0].Id;

                    TempData[Constants.MESSAGE] = "Đã thêm thành công";
                }

                return RedirectToAction("Index", "FurnitureProduct");
            }

            return View();
        }

        public ActionResult Edit(int id)
        {
            var catRepository = RepositoryManager.CreateTKNTCategoryRepository();

            var proRepository = RepositoryManager.CreateTKNTProductRepository();

            var objEdit = proRepository.GetById(id);

            if (objEdit != null)
            {
                var model = new FurnitureProductModel()
                                {
                                    Categories = catRepository.GetAll(),
                                    ViDescription = objEdit.ViDescription,
                                    EnDescription = objEdit.EnDescription,
                                    ViInvestorName = objEdit.ViInvestorName,
                                    EnInvestorName = objEdit.EnInvestorName,
                                    ViInvestorDescription = objEdit.ViInvestorDescription,
                                    EnInvestorDescription = objEdit.EnInvestorDescription,
                                    CategoryId = objEdit.Category == null ? 0 : objEdit.Category.Id,
                                    Area = objEdit.Area,
                                    IsHost = objEdit.IsHot,
                                    ProductId = objEdit.Id
                                };

                return View(model);
            }

            return View("Index");
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Edit(FurnitureProductModel productModel)
        {
            var catRepository = RepositoryManager.CreateTKNTCategoryRepository();

            var proRepository = RepositoryManager.CreateTKNTProductRepository();

            if (productModel != null)
            {
                var objProduct = proRepository.GetById(productModel.ProductId);

                if (objProduct != null)
                {
                    objProduct.Category = catRepository.GetById(productModel.CategoryId);
                    objProduct.Area = productModel.Area;
                    objProduct.ViDescription = productModel.ViDescription;
                    objProduct.EnDescription = productModel.EnDescription;
                    objProduct.ViInvestorName = productModel.ViInvestorName;
                    objProduct.EnInvestorName = productModel.EnInvestorName;
                    objProduct.ViInvestorDescription = productModel.ViInvestorDescription;
                    objProduct.EnInvestorDescription = productModel.EnInvestorDescription;
                    objProduct.IsHot = productModel.IsHost;

                    proRepository.Update(objProduct);

                    TempData[Constants.MESSAGE] = "Đã cập nhật thành công";
                }
            }

            return RedirectToAction("Index");
        }

        public ActionResult Search(string id)
        {
            var repository = RepositoryManager.CreateTKNTProductRepository();

            var lst = repository.SearchByDescription(id, Culture.Name);

            return View(lst);
        }

        #endregion action

        #region private method

        private List<tkntProduct> Paging(int pageIndex, tkntProductRepository repository)
        {
            var totalPage = repository.GetTotalPage(Constants.NUM_OF_ROWS);

            ViewData[Constants.TOTAL_PAGE] = totalPage;

            ViewData[Constants.PAGE_INDEX] = pageIndex;

            if (pageIndex > totalPage)
                return null;

            var lst = repository.Paging(pageIndex, Constants.NUM_OF_ROWS);

            return lst;
        }

        #endregion private method
    }
}